home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / samples / twotextures.c.z / twotextures.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  5.1 KB  |  241 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include "tk.h"
  10.  
  11.  
  12. GLenum doubleBuffer, directRender;
  13. int winW = 300, winH = 300;
  14.  
  15. char *earthTexFileName = 0, *skyTexFileName = 0;
  16. TK_RGBImageRec *earthImage, *skyImage;
  17. GLint skyList, earthList;
  18.  
  19. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  20. float decal[] = {GL_DECAL};
  21. float modulate[] = {GL_MODULATE};
  22. float repeat[] = {GL_REPEAT};
  23. float clamp[] = {GL_CLAMP};
  24. float nr[] = {GL_NEAREST};
  25. float ln[] = {GL_LINEAR};
  26. float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
  27. float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
  28. float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
  29. float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
  30.  
  31. int horizon;
  32. float texMinX, texMinY, texMaxX, texMaxY;
  33.  
  34.  
  35. static void Init(void)
  36. {
  37.  
  38.     earthImage = tkRGBImageLoad(earthTexFileName);
  39.     skyImage = tkRGBImageLoad(skyTexFileName);
  40.  
  41.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  42.  
  43.     magFilter = nr;
  44.     minFilter = nr;
  45.     sWrapMode = repeat;
  46.     tWrapMode = repeat;
  47.  
  48.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  49.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  50.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  51.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  52.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  53.     glEnable(GL_TEXTURE_2D);
  54.  
  55.     glClearColor(0.0, 0.0, 0.0, 0.0);
  56.  
  57.     horizon = winH / 2;
  58.  
  59.     texMinX = 0.25;
  60.     texMaxX = 0.75;
  61.  
  62.     texMinY = 0.25;
  63.     texMaxY = 0.75;
  64.  
  65.     skyList = glGenLists(1);
  66.     glNewList(skyList, GL_COMPILE);
  67.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, skyImage->sizeX, skyImage->sizeY,
  68.               GL_RGB, GL_UNSIGNED_BYTE, skyImage->data);
  69.     glEndList();
  70.  
  71.     earthList = glGenLists(1);
  72.     glNewList(earthList, GL_COMPILE);
  73.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, earthImage->sizeX, earthImage->sizeY,
  74.               GL_RGB, GL_UNSIGNED_BYTE, earthImage->data);
  75.     glEndList();
  76. }
  77.  
  78. static void Reshape(int width, int height)
  79. {
  80.  
  81.     winW = width;
  82.     winH = height;
  83.  
  84.     glViewport(0, 0, (GLint)winW, (GLint)winH);
  85.  
  86.     glMatrixMode(GL_PROJECTION);
  87.     glLoadIdentity();
  88.     gluOrtho2D(0, winW, 0, winH);
  89.     glMatrixMode(GL_MODELVIEW);
  90. }
  91.  
  92. static GLenum Key(int key, GLenum mask)
  93. {
  94.  
  95.     switch (key) {
  96.       case TK_ESCAPE:
  97.     tkQuit();
  98.  
  99.       case TK_LEFT:
  100.     texMinX -= 5.0 / (float)winW;
  101.     texMaxX -= 5.0 / (float)winW;
  102.     break;
  103.       case TK_RIGHT:
  104.     texMinX += 5.0 / (float)winW;
  105.     texMaxX += 5.0 / (float)winW;
  106.     break;
  107.  
  108.       case TK_UP:
  109.     texMinY += 5.0 / (float)winH;
  110.     texMaxY += 5.0 / (float)winH;
  111.     break;
  112.       case TK_DOWN:
  113.     texMinY -= 5.0 / (float)winH;
  114.     texMaxY -= 5.0 / (float)winH;
  115.     break;
  116.  
  117.       case TK_1:
  118.     horizon -= 5;
  119.     texMinY -= 5.0 / (float)winH;
  120.     texMinY += 5.0 / (float)winH;
  121.     texMaxY += 5.0 / (float)winH;
  122.     break;
  123.       case TK_2:
  124.     horizon += 5;
  125.     texMinY += 5.0 / (float)winH;
  126.     texMinY -= 5.0 / (float)winH;
  127.     texMaxY -= 5.0 / (float)winH;
  128.     break;
  129.  
  130.       default:
  131.     return GL_FALSE;
  132.     }
  133.     return GL_TRUE;
  134. }
  135.  
  136. static void Draw(void)
  137. {
  138.  
  139.     glClear(GL_COLOR_BUFFER_BIT);
  140.  
  141.     glPushMatrix();
  142.  
  143.     glCallList(skyList); 
  144.     glBegin(GL_POLYGON);
  145.     glTexCoord2f(texMinX, texMinY);
  146.     glVertex2i(0, horizon);
  147.     glTexCoord2f(texMaxX, texMinY);
  148.     glVertex2i(winW, horizon);
  149.     glTexCoord2f(texMaxX, texMaxY);
  150.     glVertex2i(winW, winH);
  151.     glTexCoord2f(texMinX, texMaxY);
  152.     glVertex2i(0, winH);
  153.     glEnd();
  154.  
  155.     glCallList(earthList); 
  156.     glBegin(GL_POLYGON);
  157.     glTexCoord2f(0.0, 0.0);
  158.     glVertex2i(0, 0);
  159.     glTexCoord2f(1.0, 0.0);
  160.     glVertex2i(winW, 0);
  161.     glTexCoord2f(1.0, 1.0);
  162.     glVertex2i(winW, horizon);
  163.     glTexCoord2f(0.0, 1.0);
  164.     glVertex2i(0, horizon);
  165.     glEnd();
  166.  
  167.     glPopMatrix();
  168.  
  169.     glFlush();
  170.  
  171.     if (doubleBuffer) {
  172.     tkSwapBuffers();
  173.     }
  174. }
  175.  
  176. static GLenum Args(int argc, char **argv)
  177. {
  178.     GLint i;
  179.  
  180.     doubleBuffer = GL_FALSE;
  181.     directRender = GL_TRUE;
  182.  
  183.     for (i = 1; i < argc; i++) {
  184.     if (strcmp(argv[i], "-sb") == 0) {
  185.         doubleBuffer = GL_FALSE;
  186.     } else if (strcmp(argv[i], "-db") == 0) {
  187.         doubleBuffer = GL_TRUE;
  188.     } else if (strcmp(argv[i], "-dr") == 0) {
  189.         directRender = GL_TRUE;
  190.     } else if (strcmp(argv[i], "-ir") == 0) {
  191.         directRender = GL_FALSE;
  192.     } else if (strcmp(argv[i], "-earth") == 0) {
  193.         if (i+1 >= argc || argv[i+1][0] == '-') {
  194.         printf("-earth (No file name).\n");
  195.         return GL_FALSE;
  196.         } else {
  197.         earthTexFileName = argv[++i];
  198.         }
  199.     } else if (strcmp(argv[i], "-sky") == 0) {
  200.         if (i+1 >= argc || argv[i+1][0] == '-') {
  201.         printf("-sky (No file name).\n");
  202.         return GL_FALSE;
  203.         } else {
  204.         skyTexFileName = argv[++i];
  205.         }
  206.     } else {
  207.         printf("%s (Bad option).\n", argv[i]);
  208.         return GL_FALSE;
  209.     }
  210.     }
  211.     return GL_TRUE;
  212. }
  213.  
  214. void main(int argc, char **argv)
  215. {
  216.     GLenum type;
  217.  
  218.     if (Args(argc, argv) == GL_FALSE) {
  219.     tkQuit();
  220.     }
  221.  
  222.     tkInitPosition(0, 0, winW, winH);
  223.  
  224.     type = TK_RGB;
  225.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  226.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  227.     tkInitDisplayMode(type);
  228.  
  229.     if (tkInitWindow("Two Texture Test") == GL_FALSE) {
  230.     tkQuit();
  231.     }
  232.  
  233.     Init();
  234.  
  235.     tkExposeFunc(Reshape);
  236.     tkReshapeFunc(Reshape);
  237.     tkKeyDownFunc(Key);
  238.     tkDisplayFunc(Draw);
  239.     tkExec();
  240. }
  241.